package com.mcms.web.control;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;

import com.mcms.service.UserService;
import com.mcms.service.fz.FZInfoService;
import com.mcms.service.fz.FZListService;
import com.mcms.service.fz.IsHaveFZInfoService;
import com.mcms.web.LoginCommand;
import com.mcms.web.vo.DWZResult;
import com.mcms.web.vo.DictCache;
import com.mcms.web.vo.PageAction;

/**
 * 分诊管理
 * @author 
 */
public class SeparateManager extends AbstractCommandController {
	private UserService userService;

	public SeparateManager() {
		setCommandClass(LoginCommand.class);
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	protected ModelAndView handle(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		System.out.println("Begin:SeparateManager ");
		/**参数获取*/
		String actionType = request.getParameter("actionType");// 控制跳转参数
		String userName = request.getParameter("userName");
		String deptCode = request.getParameter("deptCode");
		String customid = request.getParameter("customid");
		String doctorId = request.getParameter("doctorId");
		String appointDate = request.getParameter("appointDate");
		String appointDate1 = request.getParameter("appointDate1");
		String flag = request.getParameter("flag");
		String pageNum = request.getParameter("pageNum");
		String numPerPage = request.getParameter("numPerPage");
		String orderField = request.getParameter("orderField");
		String orderDirection = request.getParameter("orderDirection");
		PageAction pageAction = (PageAction) request.getAttribute("pageAction");
		
		String deptId = request.getParameter("deptId");
		Map sessionMap = (Map) request.getSession().getAttribute("mcmsSession");
		Map paramMap = new HashMap();
		paramMap.put("customid", customid);// 客户号
		paramMap.put("doctorId", doctorId);// 就诊号
		paramMap.put("appointDate",appointDate);
		paramMap.put("appointDate1",appointDate1);
		paramMap.put("flag",flag);
		
		paramMap.put("pageNum", pageNum);
		paramMap.put("numPerPage", numPerPage);
		paramMap.put("orderField", orderField);
		paramMap.put("orderDirection", orderDirection);
		paramMap.put("pageAction", pageAction);
		
		
		System.out.println("at:"+actionType+";un:"+userName+";customid:"+customid+";deptId:"+deptId);
		
		/**登陆页跳转*/
		if(actionType!=null && actionType.trim().length()>0){
			if(actionType.equals("listSeparateCustom")){
				
				// 查询分诊列表
				Map resultMap =  new FZListService().execute(sessionMap, paramMap);
				request.setAttribute("resultMap", resultMap);
				return new ModelAndView("custom/fzdepart/separateCustomList");
				
			}else if(actionType.equals("detailCustomPage")){
				
				// 列表中点击“分诊”按钮，跳转至分诊页面:根据客户ID和就诊号查询是否已经分诊，如已经分诊则显示所分诊医生及部门
//				String doctorid = request.getParameter("doctorid");
//				paramMap.put("customid", customid);
//				paramMap.put("doctorid", doctorId);

				Map resultMap = (Map) new IsHaveFZInfoService().execute(sessionMap, paramMap);
				request.setAttribute("resultMap", resultMap);
				request.setAttribute("customid", customid);
				request.setAttribute("doctorId", doctorId);
				return new ModelAndView("custom/fzdepart/detailSepareteCustomPage");
				
			}else if(actionType.equals("showDeptUser")){
				
				// 根据deptId获取对应的用户列表
				Map map = (Map)DictCache.getInstance().getDictMapByType("DEPTUSER");
				Map userMap = (Map)map.get(Integer.parseInt(deptId));
				Set setInner =userMap.keySet();//       
				Iterator it=setInner.iterator();//   
				StringBuffer sb = new StringBuffer("[");
				while(it.hasNext()){//     
					Object key = (Object) it.next();
					userMap.get(key);
					sb.append("[\""+key+"\",\""+userMap.get(key)+"\"],");
					
				}
				sb.deleteCharAt(sb.length()-1);// 删除最后一个逗号
				sb.append("]");
				request.setAttribute("result", sb.toString());
				return new ModelAndView("custom/fzdepart/combox/staff");
				
			}else if(actionType.equals("insertOrUpdateInfo")){
				String remark = request.getParameter("remark");
				String dealDoctor = request.getParameter("dealDoctor");
				
				
				paramMap.put("remark", remark);// 分诊备注信息
				paramMap.put("dealDoctor", dealDoctor);// 处置医生ID
				
				// 保存分诊信息
				DWZResult dwzResult =  new FZInfoService().execute(sessionMap, paramMap);
				
				// 添加结果信息
//				dwzResult.setMessage((String)paramMap.get("message"));// resultMsg
//				dwzResult.setNavTabId("aa");// result rel
//				boolean isSuc = (Boolean)paramMap.get("isSuc");
//				String statusCode = "200";// 成功
//				if(!isSuc){
//					statusCode = "300";// 失败
//				}
//				dwzResult.setStatusCode(statusCode);// resultSuc
				request.setAttribute("dwzResult", dwzResult);
				return new ModelAndView("result");
			}
		}
		
		/**默认跳转至登陆页面*/
		return new ModelAndView("login");

	}
}

